home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / htmlsample / sampleutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  7.7 KB  |  241 lines

  1. /*
  2.     file SampleUtils.c
  3.     
  4.     Description:
  5.     This file contains a number of utility routines used by the
  6.     HTMLSampleApplication.  These routines have been moved
  7.     here to a separate file to simplify the example.
  8.     
  9.     HTMLSample is an application illustrating how to use the new
  10.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  11.     is Apple's light-weight HTML rendering engine capable of
  12.     displaying HTML files.
  13.  
  14.     by John Montbriand, 1999.
  15.  
  16.     Copyright: © 1999 by Apple Computer, Inc.
  17.     All rights reserved.
  18.     
  19.     Disclaimer:
  20.     You may incorporate this sample code into your applications without
  21.     restriction, though the sample code has been provided "AS IS" and the
  22.     responsibility for its operation is 100% yours.  However, what you are
  23.     not permitted to do is to redistribute the source as "DSC Sample Code"
  24.     after having made changes. If you're going to re-distribute the source,
  25.     we require that you make it clear in the source that the code was
  26.     descended from Apple Sample Code, but that you've made changes.
  27.     
  28.     Change History (most recent first):
  29.     10/16/99 created by John Montbriand
  30. */
  31.  
  32. #include "SampleUtils.h"
  33. #include <Processes.h>
  34. #include <string.h>
  35. #include <QuickDraw.h>
  36. #include <Gestalt.h>
  37. #include <Palettes.h>
  38. #include <Resources.h>
  39.  
  40. #include <HTMLRendering.h>
  41.  
  42.  
  43.  
  44. /* SetWindowStandardStateSize sets the window's standard state rectangle
  45.     to a rectangle of the size suggested in the width and height parameters.
  46.     In the end, it may set the standard size to something smaller than the
  47.     width and height parameters so the entire window remains visible.  The
  48.     standard rectangle is also centered on the main screen.  The window's
  49.     standard state rectangle is used by ZoomWindow whenever when it
  50.     is called with the inZoomOut partcode. */
  51. void SetWindowStandardStateSize(WindowPtr window, short width, short height) {
  52.     Rect standard, stdRect, global, device, c, s, diffs;
  53.     RgnHandle sRgn, cRgn;
  54.     GDHandle theDevice;
  55.         /* make the window's port the current port */
  56.     SetPort(window);
  57.         /* find the window's monitor coordinates */
  58.     global = window->portRect;
  59.     LocalToGlobal((Point*) &global.top);
  60.     LocalToGlobal((Point*) &global.bottom);
  61.         /* get the maximum intersecting screen */
  62.     theDevice = GetMaxDevice(&global);
  63.     device = (**theDevice).gdRect;
  64.         /* if it's the main screen, adjust it for the menu bar height */
  65.     if (theDevice == GetMainDevice())
  66.         device.top += GetMBarHeight();
  67.         /* calculate the difference between the window's content rectangle and its frame */
  68.     sRgn = NewRgn();
  69.     cRgn = NewRgn();
  70.     GetWindowStructureRgn(window, sRgn);
  71.     GetWindowContentRgn(window, cRgn);
  72.     c = (**cRgn).rgnBBox;
  73.     s = (**sRgn).rgnBBox;
  74.     SetRect(&diffs, c.left - s.left + 2, c.top - s.top + 2, s.right - c.right + 2, s.bottom - c.bottom + 2);
  75.     DisposeRgn(sRgn);
  76.     DisposeRgn(cRgn);
  77.         /* calculate the maximum bounds for the standard rectangle */
  78.     SetRect(&standard, device.left + diffs.left, device.top + diffs.top, device.right - diffs.right, device.bottom - diffs.bottom);
  79.         /* set up our 'desired' rectangle */
  80.     SetRect(&stdRect, 0, 0, width, height);
  81.     OffsetRect(&stdRect, standard.left, standard.top);
  82.         /* fit it inside of the maximum allowable rectangle */
  83.     SectRect(&stdRect, &standard, &stdRect);
  84.         /* center it in the maximum allowable rectangle */
  85.     OffsetRect(&stdRect, (standard.right - stdRect.right)/2, (standard.bottom - stdRect.bottom)/2);
  86.         /* set the standard state rectangle */
  87.     SetWindowStandardState(window, &stdRect);
  88. }
  89.  
  90. #include <StdIO.h>
  91. #include <StdArg.h>
  92. #include <TextUtils.h>
  93. void debugf(char const *fmt, ...);
  94. void debugf(char const *fmt, ...) {
  95.     char s[256];
  96.     va_list args;
  97.     va_start(args, fmt);
  98.     s[0] = vsprintf(s+1, fmt, args);
  99.     va_end(args);
  100.     DebugStr((StringPtr) s);
  101. }
  102.  
  103.  
  104. /* GetApplicationFolder returns a URL referring to the folder
  105.     containing the application.  If successful, *url is set
  106.     to a new handle containing the URL.  It is the caller's
  107.     responsibility to dispose of this handle. */
  108. OSStatus GetApplicationFolderURL(Handle *url) {
  109.     OSStatus err;
  110.     FSSpec spec;
  111.     Handle theURL;
  112.         /* set up locals to a known state */
  113.     theURL = NULL;
  114.         /* find the application's folder */ 
  115.     err = GetApplicationFolder(&spec);
  116.     if (err != noErr) goto bail;
  117.         /* create a new handle for storing the URL */
  118.     theURL = NewHandle(0);
  119.     if (theURL == NULL) { err = memFullErr; goto bail; }
  120.         /* ask the HTML rendering library to convert
  121.         the FSSpec into a URL */
  122.     err = HRUtilGetURLFromFSSpec(&spec, theURL);
  123.     if (err != noErr) goto bail;
  124.     
  125.         /* add a slash at the end of the name, if one is not there */
  126.     if ( (*theURL)[GetHandleSize(theURL) - 1] != '/')
  127.         Munger(theURL, (GetHandleSize(theURL) - 1), NULL, 0, "/", 1);
  128.  
  129.         /* return the URL */
  130.     *url = theURL;
  131.     return noErr;
  132. bail:
  133.     if (theURL != NULL) DisposeHandle(theURL);
  134.     return err;
  135. }
  136.  
  137.  
  138. /* GetApplicationFolder returns the volume reference number and
  139.     directory id of the folder containing the application. */
  140. OSStatus GetApplicationFolder(FSSpec *spec) {
  141.     OSStatus err;
  142.     FCBPBRec fcpb;
  143.     Str255 name;
  144.     CInfoPBRec cat;
  145.     Handle h;
  146.     
  147.     h = GetResource('vTeZ', 0);
  148.     if (h == NULL) return resNotFound;
  149.     
  150.     memset(&fcpb, 0, sizeof(fcpb));
  151.     fcpb.ioVRefNum = 0;
  152.     fcpb.ioNamePtr = name;
  153.     fcpb.ioFCBParID = 0;
  154.     fcpb.ioRefNum = HomeResFile(h);
  155.     fcpb.ioFCBIndx = 0;
  156.     if ((err = PBGetFCBInfoSync(&fcpb)) != noErr) return err;
  157.     
  158.     memset(&cat, 0, sizeof(cat));
  159.     cat.dirInfo.ioNamePtr = name;
  160.     cat.dirInfo.ioVRefNum = fcpb.ioFCBVRefNum;
  161.     cat.dirInfo.ioFDirIndex = -1;
  162.     cat.dirInfo.ioDrDirID = fcpb.ioFCBParID;
  163.     err = PBGetCatInfoSync(&cat);
  164.     if (err != noErr) return err;
  165.     
  166.     err = FSMakeFSSpec(fcpb.ioFCBVRefNum, cat.dirInfo.ioDrParID, name, spec);
  167.     if (err != noErr) return err;
  168.     
  169.     return noErr;
  170. }
  171.  
  172. /* DrawGrowIconWithoutScrollLines draws the grow icon in the bottom
  173.     right corner of the frontmost window without drawing the scroll bar
  174.     lines.  It does this by setting the clip region to the bottom right corner
  175.     before calling DrawGrowIcon. */
  176. void DrawGrowIconWithoutScrollLines(WindowPtr window) {
  177.     RgnHandle clip;
  178.     Rect r = { 0, 0, 16, 16 };
  179.         /* set the window to the current port */
  180.     SetPort(window);
  181.         /* save the old clipping region */
  182.     clip = NewRgn();
  183.     if (clip == NULL) return;
  184.     GetClip(clip);
  185.         /* set the clipping region to the bottom right corner of the window */
  186.     OffsetRect(&r, window->portRect.right-15, window->portRect.bottom-15);
  187.     ClipRect(&r);
  188.         /* draw the grow icon */
  189.     DrawGrowIcon(window);
  190.     SetPort(window);
  191.     SetClip(clip);
  192.     DisposeRgn(clip);
  193. }
  194.  
  195.  
  196.  
  197.  
  198. /* GrayOutBox grays out an area of the screen in the current grafport.  
  199.     *theBox is in local coordinates in the current grafport. This routine
  200.     is for direct screen drawing only.  */
  201. void GrayOutBox(Rect *theBox) {
  202.     long response;
  203.     Rect globalBox;
  204.     GDHandle maxDevice;
  205.     RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF}, rgbBlack = {0, 0, 0}, sForground, sBackground;
  206.     PenState penSave;
  207.         /* save the current drawing state */
  208.     GetPenState(&penSave);
  209.         /* if no color quickdraw, fail...*/
  210.     if (Gestalt(gestaltQuickdrawVersion, &response) != noErr) response = 0;
  211.     if (response >= gestalt8BitQD) {
  212.             /* get the device for the rectangle */
  213.         globalBox = *theBox;
  214.         LocalToGlobal((Point*) &globalBox.top);
  215.         LocalToGlobal((Point*) &globalBox.bottom);
  216.         maxDevice = GetMaxDevice(&globalBox);
  217.         if (maxDevice != NULL) {
  218.                 /* calculate the best gray */
  219.             if ( GetGray(maxDevice, &rgbWhite, &rgbBlack)) {
  220.                     /* draw over the area in gray using addMax transfer mode */
  221.                 GetForeColor(&sForground);
  222.                 GetBackColor(&sBackground);
  223.                 RGBForeColor(&rgbBlack);
  224.                 RGBBackColor(&rgbWhite);
  225.                 PenMode(addMax);
  226.                 PaintRect(theBox);
  227.                 RGBForeColor(&sForground);
  228.                 RGBBackColor(&sBackground);
  229.                     /* restore the pen state and leave */
  230.                 SetPenState(&penSave);
  231.                 return;
  232.             }
  233.         }
  234.     }
  235.         /* fall through to using the gray pattern */
  236.     PenPat(&qd.gray);
  237.     PenMode(notPatBic);
  238.     PaintRect(theBox);
  239.     SetPenState(&penSave);
  240. }
  241.